package cn.tedu.smartos.entertainment.service.impl;

import cn.tedu.smartos.base.exception.ServiceException;
import cn.tedu.smartos.entertainment.mapper.EntertainmentMapper;
import cn.tedu.smartos.entertainment.pojo.dto.EntertainmentAddParam;
import cn.tedu.smartos.entertainment.pojo.dto.EntertainmentCategoryAddParam;
import cn.tedu.smartos.entertainment.pojo.entity.EntertainmentCategory;
import cn.tedu.smartos.entertainment.pojo.entity.EntertainmentResource;
import cn.tedu.smartos.entertainment.pojo.vo.EntertainmentCategoryVO;
import cn.tedu.smartos.entertainment.pojo.vo.EntertainmentResourceVO;
import cn.tedu.smartos.entertainment.service.EntertainmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EntertainmentServiceImpl implements EntertainmentService {
    @Autowired
    private EntertainmentMapper entertainmentMapper;

    /**
     * 获取娱乐分类列表
     *
     * @param pageNum  页码
     * @param pageSize 每页记录数
     * @return 娱乐分类列表
     */
    @Override
    public PageInfo<EntertainmentCategoryVO> listCategory(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【获取娱乐分类列表】的请求,参数{},{}",pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<EntertainmentCategoryVO> entertainmentCategoryVOS = entertainmentMapper.listCategory();
        return new PageInfo<>(entertainmentCategoryVOS);
    }
    /**
     * 添加娱乐分类
     *
     * @param addParam 添加娱乐分类参数
     */
    @Override
    public void addCategory(EntertainmentCategoryAddParam addParam) {
        log.debug("开始处理【添加娱乐分类】的请求,参数{}",addParam);
        EntertainmentCategory category = new EntertainmentCategory();
        BeanUtils.copyProperties(addParam, category);
        category.setCreateTime(new Date());
        Integer rows = entertainmentMapper.addCategory(category);
        if (rows != 1){
            throw new ServiceException("添加娱乐分类失败");
        }
    }

    /**
     * 删除娱乐分类
     * @param categoryIds 分类ID集合
     */
    @Override
    public void deleteCategory(List<Integer> categoryIds) {
        log.debug("开始处理【删除娱乐分类】的请求,参数{}",categoryIds);
        if (CollectionUtils.isEmpty(categoryIds)){
            throw new ServiceException("参数categoryIds不能为空");
        }
        Integer rows = entertainmentMapper.deleteCategory(categoryIds);
        if (rows == 0){
            throw new ServiceException("删除出错");
        }
        if (rows != categoryIds.size()){
            throw new ServiceException("部分分类删除失败");
        }
    }

    /**
     * 娱乐分类详情
     * @param categoryId 分类ID
     * @return 分类详情
     */
    @Override
    public EntertainmentCategoryVO detailCategory(Integer categoryId) {
        log.debug("开始处理【娱乐分类详情】的业务，参数：{}", categoryId);
        if (categoryId == null){
            throw new ServiceException("分类ID不能为空");
        }
        EntertainmentCategoryVO category = entertainmentMapper.detailCategory(categoryId);
        return category;
    }

//————————————————————————————————————————————————————————————————————

    /**
     * 添加娱乐资源
     * @param addParam 添加娱乐资源参数
     */
    @Override
    public void addEntertainment(EntertainmentAddParam addParam) {
        log.debug("开始处理【添加娱乐资源】的请求,参数{}",addParam);
        EntertainmentResource enter = new EntertainmentResource();
        BeanUtils.copyProperties(addParam,enter);
        enter.setCreateTime(new Date());
        enter.setUpdateTime(new Date());
        Integer rows = entertainmentMapper.addEntertainment(enter);
        if (rows != 1){
            throw new ServiceException("添加失败");
        }
    }

    /**
     * 获取娱乐资源列表
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return PageInfo<EntertainmentResourceVO>
     */
    @Override
    public PageInfo<EntertainmentResourceVO> listEntertainment(Integer categoryId,Integer pageNum, Integer pageSize) {
        log.debug("开始处理【获取娱乐资源列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<EntertainmentResourceVO> list = entertainmentMapper.listEntertainment(categoryId);
        return new PageInfo<>(list);
    }
    /**
     * 删除娱乐资源
     * @param resourceIds 娱乐资源ID列表
     */
    @Override
    public void deleteEntertainment(List<Long> resourceIds) {
        log.debug("开始处理【删除娱乐资源】的请求,参数{}",resourceIds);
        if (CollectionUtils.isEmpty(resourceIds)) {
            throw new IllegalArgumentException("娱乐资源ID列表不能为空");
        }
        Integer rows = entertainmentMapper.deleteEntertainment(resourceIds);
        if (rows == 0) {
            throw new ServiceException("删除失败或娱乐资源不存在");
        }
        if (rows != resourceIds.size()) {
            throw new ServiceException("部分娱乐资源未能删除");
        }
    }

    /**
     * 娱乐资源详情
     * @param resourceId 娱乐资源ID
     * @return 娱乐资源详情
     */
    @Override
    public EntertainmentResourceVO detail(Long resourceId) {
        log.debug("开始处理【获取娱乐资源详情】的业务，参数：{}", resourceId);
        if (resourceId == null){
            throw new ServiceException("参数resourceId不能为空");
        }
        EntertainmentResourceVO entertainmentResourceVO = entertainmentMapper.detail(resourceId);
        return entertainmentResourceVO;
    }
}
